package com.imooc.diveinspringboot.listener;

import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.Environment;

import java.util.Iterator;
import java.util.List;

/**
 * Before ConfigFileApplicationListener 实现
 */
public class BeforeConfigFileApplicationListener implements SmartApplicationListener,Ordered {

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType) || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
    }

    @Override
    public int getOrder() { // 比ConfigFileApplicationListener优先级更高(-1不输出， +1输出)
        return ConfigFileApplicationListener.DEFAULT_ORDER - 1;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            //this.onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent)event);
            ApplicationEnvironmentPreparedEvent preparedEvent = (ApplicationEnvironmentPreparedEvent)event;
            Environment environment = preparedEvent.getEnvironment();
            System.out.println("environment.getProperty(\"name\"):" + environment.getProperty("name"));
        }

        if (event instanceof ApplicationPreparedEvent) {
            //this.onApplicationPreparedEvent(event);
        }

    }
}
